//jQuery(function($){
//  'use strict';
//
//  // -------------------------------------------------------------
//  //   Basic Navigation
//  // -------------------------------------------------------------
//  (function () {
//      var $frame  = $('#basic');
//      var $slidee = $frame.children('ul').eq(0);
//      var $wrap   = $frame.parent();
//
//      // Call Sly on frame
//      $frame.sly({
//          horizontal: 1,
//          itemNav: 'basic',
//          smart: 1,
//          // activateOn: 'click',
//          mouseDragging: 1,
//          touchDragging: 1,
//          releaseSwing: 1,
//          startAt: -1,i
//          // scrollBar: $wrap.find('.scrollbar'),
//          scrollBy: 1,
//          // pagesBar: $wrap.find('.pages'),
//          // activatePageOn: 'click',
//          speed: 300,
//          swingSpeed:    0,
//          elasticBounds: 1,
//          easing: 'easeOutExpo',
//          dragHandle: 1,
//          // dynamicHandle: 1,
//          // clickBar: 1,
//
//          // Buttons
//          // forward: $wrap.find('.forward'),
//          // backward: $wrap.find('.backward'),
//           prev: $('.prev'),
//           next: $('.next'),
//          // prevPage: $wrap.find('.prevPage'),
//          // nextPage: $wrap.find('.nextPage')
//      });
//
//  }());
//
//
//});
$slider = $('#basic');
$speed = 300;
$process = 0;
$effect = 'linear';//http://api.jqueryui.com/easings/
$ul = $slider.find('ul:first');


$first = $slider.find('li:first');
$width = $first.outerWidth();
$eachSlide = 1;
//$margin = $first.next().offset().left - $width - $first.offset().left;
$margin = 10;
$perSlideDrag = 3;
$scrollMax = $slider.width() - $slider.width();
$eachWidth = $width + $margin ;
$ulWidth = $ul.find('li').length * $eachWidth+1 ;
$maxLi = $ul.find('li').length;
$wrap = $slider.find('#slider-wrap');
$ul.css('width', $ulWidth  + 'px');
$limitTime = 300;
$device = 'pc';

function windowResize() {
    $ul = $slider.find('ul:first');
    $first = $slider.find('li:first');
    $width = $first.outerWidth();
    $eachWidth = $width + $margin;
    $ulWidth = $ul.find('li').length * $eachWidth +1;
    $sliderWidth = $('html').width() ;
    $slider.css('width', $sliderWidth + 'px');
    $scrollMax = $slider.width() - $slider.width();
    $slider.find('ul').css('width', $ulWidth  + 'px');
    if (checkSlider() && $slider.find('ul').length < 3) {

        $slider.find('ul').clone().appendTo($wrap);
        $slider.find('ul').clone().prependTo($wrap);
        $slider.find('ul').eq(1).addClass('main-slide');
        $wrap.css('width', ($slider.find('ul').length * $ulWidth + $slider.find('ul').length) + 'px');
        $slider.scrollLeft($ulWidth + $margin);

    }
    else if (!checkSlider() && $slider.find('ul').length >= 3) {
        $slider.find('ul').not(':first').remove();
        $ul = $slider.find('ul');
        $wrap.css('width', '100%');
        $slider.scrollLeft($margin);
    }
    viewdetail();
    slideCenter();
}
function checkSlider() {
    if ($slider.find('ul').width() <= ($slider.width() + $margin)) {
        $process = 0;
        return false;
    }
    return true;
}
function slideCenter() {
    $slider.scrollLeft($ulWidth + $slider.scrollLeft() % $ulWidth);

}
function fixslide() {
    if ($process) {
        return;
    }

    $process = 1;
    $scleft = $slider.scrollLeft();
    $scrollWidth = $scleft - $scleft % $eachWidth;

    $slider.animate({
        'scrollLeft': $scrollWidth,
    }, $speed, $effect, function() {
        $process = 0;
        slideCenter();
        $.process_slider = 0;
    });
}
function slideNext($count) {
    if ($process) {
        return;
    }

    $process = 1;
    $scrleft = $slider.scrollLeft();
    $scrollWidth = $scrleft + $count * ($eachWidth);
    $scrollWidth = $scrollWidth - $scrollWidth % $eachWidth + $margin;
    $slider.animate({
        'scrollLeft': $scrollWidth,
    }, $speed, $effect, function() {
        $process = 0;
        slideCenter();
        $.process_slider = 0;
    });
}
function slidePrev($count) {
    if ($process) {
        return;
    }

    $process = 1;
    $scrleft = $slider.scrollLeft();
    $scrollWidth = $scrleft - $count * ($eachWidth);
    $scrollWidth = $scrollWidth - $scrollWidth % $eachWidth + $margin;
    $slider.animate({
        'scrollLeft':  $scrollWidth,
    }, $speed, $effect, function() {
        $process = 0;
        slideCenter();
        $.process_slider = 0;
    });
}
function scrollNext($next) {
    if ($process) {
        return;
    }

    $process_s = 1;
    $scrollNow = $next + $slider.scrollLeft() ;
    if ($scrollNow < 0 || $scrollNow > $scrollMax) {

        slideCenter();
    }
    $slider.animate({'scrollLeft': '+=' + $next}, 0);
    $process = 0;
}
function getCountByTime($distance_x, $distance_time) {
    if ($distance_time > $limitTime) {
        return 0;
    }

    $count = parseInt(Math.abs($perSlideDrag * $distance_x / $distance_time));
    if ($count > 0 && $count < 1) {
        $count = 1;
    }
    return $count;
}
$begin = 0;
function pcdevice() {

    $slider.swipe({
        swipeStatus: function(event, phase, direction, $distance, $distance_time, fingerCount)
        {
            if (!checkSlider()) {
                return;
            }
            if (phase === 'start') {
                $begin = $distance;
                return;
            }
            if (phase === 'cancel') {
                $.process_slider = 0;
                return;
            }

            if (phase === 'move') {
                $.process_slider = 1;
                $distance_x = $distance - $begin;
                $begin = $distance;
                if (direction === 'right') {
                    $distance_x = -$distance_x;
                }
                scrollNext($distance_x);
                return;
            }
            if (phase === 'end') {
                if ($distance == 0) {
                    $.process_slider = 0;
                    return;
                }
                //alert(touch.pageX);
                $.process_slider = 1;
                $count = getCountByTime($distance, $distance_time);
                if ($count > $maxLi) {
                    $count = $maxLi;
                }
                if (direction === 'left') {
                    $distance = -$distance;
                    if ($count === 0) {
                        $count = 1;
                    }
                }

                ;



                if ($distance < 0) {

                    slideNext($count);

                }
                else if ($distance > 0) {

                    slidePrev($count);
                }
                $.process_slider = 0;
            }
            //Here we can check the:
            //phase : 'start', 'move', 'end', 'cancel'
            //direction : 'left', 'right', 'up', 'down'
            //distance : Distance finger is from initial touch point in px
            //duration : Length of swipe in MS
            //fingerCount : the number of fingers used
        },
        threshold: 0,
        maxTimeThreshold: 0,
        fingers: 'all',
        allowPageScroll: "vertical"
    });

}

jQuery(function($) {

    windowResize();
    $(window).bind('resize', function() {
        var field = $('#datepicker');
        if ($('#ui-datepicker-div:visible').length) {
            field.datepicker('hide').datepicker('show');
        }
        windowResize();

    });
    $slider.bind('mousewheel DOMMouseScroll', function(e) {
        if (!checkSlider()) {
            return;
        }
        if (e.originalEvent) {
            if (e.originalEvent.wheelDelta)
                e.delta = e.originalEvent.wheelDelta / -40;
            if (e.originalEvent.deltaY)
                e.delta = e.originalEvent.deltaY;
            if (e.originalEvent.detail)
                e.delta = e.originalEvent.detail;
        }
        $delta = e.delta;
        if ($delta > 0) {
            slideNext($eachSlide);
        }
        else {
            slidePrev($eachSlide);
        }
    });
    $('html').bind('keydown', function(e) {
        if (!checkSlider()) {
            return;
        }
        if (e.keyCode === 39 || e.keyCode === 38) {
            if ($('.fancybox-lock').length) {
                $('.event-details .view-item').stop().animate({
                    'scrollTop': '-=300'
                }, 200);
                return;
            }
            slideNext($eachSlide);
        }
        if (e.keyCode === 37 || e.keyCode === 40) {
            if ($('.fancybox-lock').length) {
                $('.event-details .view-item').stop().animate({
                    'scrollTop': '+=300'
                }, 200);
                return;
            }
            slidePrev($eachSlide);
        }
        if (e.keyCode === 13) {
            if ($('.fancybox-lock').length) {
                return;
            }
            $index = (parseInt)(($slider.scrollLeft()) / $eachWidth);
            showdetail($('.show-event-ajax').eq($index));
        }
    });
    $('.search_inner .text').bind('keydown', function(e) {
        if (e.keyCode === 13) {
            $(this).closest('form').submit();
            return false;
        }
    });
    pcdevice();
    if ($item_id) {
        //showdetail($('.show-event-ajax[item_id=' + $item_id + ']'));
    }

});
var showSpinner = 1;
var $request = {
    ajaxSubmit: function($btn, $callback) {
        _form = $btn.closest('form');
        _action = _form.attr('action');
        data = _form.serialize();
        $.ajax({
            url: _action,
            type: _form.attr('method'),
            data: data,
            dataType: "html",
            success: function(result) {
                //$request.fetchData(result);
                if (typeof($callback) === "function")
                {
                    $callback(result);
                }
            },
            error: function() {
                //window.location.reload();
            }
        });
    },
    ajaxData: function($link, $data, $callback, $method) {
        if (typeof($method) === 'undefined') {
            $method = 'GET';
        }
        $.ajax({
            url: $link,
            type: $method,
            data: $data,
            dataType: "html",
            success: function(result) {
                $request.fetchData(result);
                if (typeof($callback) === "function")
                {
                    $callback(result);
                }
            }
        });
    },
    fetchData: function(result) {
        if (result) {
            for (var item in result)
            {
                $(item).replaceWith(result[item]);
            }
        }
    }
};
